package com.jingdianjichi.oss.config;

import com.jingdianjichi.oss.service.StorageService;
import com.jingdianjichi.oss.service.impl.AliStorageServiceImpl;
import com.jingdianjichi.oss.service.impl.MinioStorageServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/*
    把我们想要的类在这里吐出去
 */
@Configuration
@RefreshScope
public class StorageConfig {

    @Value("${storage.service.type}")
    private String storageType;

    //把所有的处理器注入进来
    @Resource
    private AliStorageServiceImpl aliStorageService;

    @Resource
    private MinioStorageServiceImpl minioStorageService;


    //切换不同的文件处理器,使用配置文件,来切换
    @Bean
    @RefreshScope
    public StorageService storageService(){
        if ("minio".equals(storageType))
        {
            return minioStorageService;
        }
        else if ("aliyun".equals(storageType))
        {
            return aliStorageService;
        }
        else
        {
            //这个异常就是用于处理非法参数的
            throw new IllegalArgumentException("未找到对应的文件存储处理器");
        }
    }
}
